<template>
    <div class="order-box">
        <ul class="order-tab fs14 flex-row plr15">
            <li class="tab-item " v-for="(item,index) in tabList" :class="{'tab-item-active':index==orderActive}"
                @click="choiceTab(index,item.status)">{{item.title}}
            </li>
        </ul>
        <better-scroll
            ref="scroll"
            class="scroll-t"
            :refresh="initDataList"
            :onLoading="loadDataList">
            <div class="order-item" v-for="item in dataList">
                <div class="flex-row plr15 order-item-head">
                    <span>订单编号：{{item.id}}</span>
                    <span class="theme-color">{{item.status_name}}</span>
                </div>
                <div class="list-item plr15" v-for="(attr, index) in item.items">
                    <div class="flex-column list-content">
                        <div class="item-list-main flex-row ptb10">
                            <img :src="attr.prd_img" alt="">
                            <div class="flex-1 item-list-intr flex-column">
                                <p class="row2 fs14 item-list-name ">{{attr.prd_name}}</p>
                                <div class="flex-row list-item-abreast">
                                    <!--<p class="row1" @click.stop="aaa">颜色：12#川南玛瑙</p>-->
                                </div>
                                <span class="item-list-price theme-color">¥{{computePrice(attr.prd_price)}}</span>
                                <span class="item-list-num">X{{attr.num}}</span>
                            </div>
                        </div>
                        <li class="status-item status-item-active status-evaulate" v-if="item.status_name=='待评价'"
                            @click="$router.push({name:'order-evaulate',query:{oid:item.id,item:JSON.stringify(attr)}})">
                            <span>去评价</span>
                        </li>
                    </div>

                </div>
                <div class="order-bottom plr15">
                    <div class="order-item-amount flex-row">
                        <span>合计：¥{{computePrice(item.money)}}</span>
                        <!--<span>含运费：10.00</span>-->
                    </div>
                    <ul class="order-item-status flex-row">
                        <li class="status-item status-item-active" v-if="item.status_name=='待收货'"
                            @click="receipt(item.id)">
                            <span>确认收货</span>
                        </li>
                        <li class="status-item status-item-active" v-if="item.status_name=='待收货'"
                            @click="returnGoods(item.id)">
                            <span>申请退货</span>
                        </li>
                        <!--<li class="status-item status-item-active" v-if="item.status_name=='待评价'"-->
                        <!--@click="$router.push({name:'order-evaulate',query:{oid:item.id,prdID:item.prdID}})">-->
                        <!--<span>去评价</span>-->
                        <!--</li>-->
                    </ul>
                </div>
            </div>
        </better-scroll>
    </div>
</template>

<script>
    import {mapGetters} from "vuex"

    export default {
        name: "OrderList",
        data() {
            return {
                tabList: [
                    {title: '全部', status: null},
                    {title: '待付款', status: 9},
                    {title: '待发货', status: 0},
                    {title: '待收货', status: 1},
                    {title: '待评价', status: 2},
                ],
                dataList: [],
                params: {offset: 0, len: 20}

            }
        },
        computed: {
            ...mapGetters(['orderActive'])
        },
        created() {
            this.tabList.forEach((item, index) => {
                if (this.orderActive == index) return this.choiceTab(this.orderActive, item.status)
            })

        },
        methods: {
            initParams() {
                this.params.offset = 0
                this.dataList = []
            },
            initDataList() {
                this.initParams()
                this.loadDataList()
            },
            loadDataList() {
                this.$store.dispatch(types.ORDER_LIST, this.params).then(res => {
                    let data = res.data
                    this.dataList = this.dataList.concat(data);
                    this.params.offset = this.dataList.length;
                    this.$refs.scroll.finish(data.length, this.params.len);
                })
            },
            choiceTab(index, status) {
                this.params.status = status
                if (status == null) delete this.params['status']
                this.$store.commit(types.SET_ORDER_STATUS, index)
                this.initDataList()
            },
            //确认收货
            receipt(oid) {
                let that = this
                utils.dialog.confirm({
                    content: "确定收货吗？",
                    confirm: function () {
                        that.$store.dispatch(types.ORDER_RECEIPT, {oid: oid}).then(res => {
                            console.log(res)
                            if (res.code !== 0) return that.fail(res.msg)
                            that.initDataList()
                        })
                    }
                });
            },
            //申请退货
            returnGoods(oid) {
                let that = this
                utils.dialog.confirm({
                    content: "确定申请退货吗？",
                    confirm: function () {
                        that.$store.dispatch(types.ORDER_REJECT, {oid: oid}).then(res => {
                            console.log(res)
                            if (res.code !== 0) return that.fail(res.msg)
                            that.initDataList()
                        })
                    }
                });
            }
        }
    }
</script>

<style scoped lang="scss">
    .scroll-t {
        top: 92px;
    }

    .order-box {
        overflow: scroll;
        /*-webkit-overflow-scrolling: touch;*/
        /*height:600px;*/
    }

    .order-item {
        background: #fff;
        margin-bottom: 10px;
        overflow: scroll;
        padding-bottom: 10px;
    }

    .order-item-head {
        height: 40px;
        border-bottom: 1px solid #efefef;
        font-size: 14px;
    }

    .list-item {
        margin-bottom: 0;
        height: auto;
        .list-content {
            padding-bottom: 5px;
            align-items: flex-end;
            width: 100%;
        }
    }

    .item-list-main {
        padding-right: 0;
    }

    .order-bottom {
        border-top: 1px solid #e8e6e6
    }

    .order-item-amount {
        justify-content: flex-end;
        height: 30px;
        span:first-child {
            padding-right: 5px;
            font-weight: bold;
        }
    }

    .order-item-status {
        justify-content: flex-end;
    }

    .status-evaulate {
        padding-left: 1px;
        padding-right: 1px;
    }

    .status-item {
        padding: 3px 8px;
        border-radius: 15px;
        color: #fff;
        background: $theme-color;
        font-size: 12px;
        margin-left: 5px;
    }

    .status-item-active {
        color: #666;
        border: 1px solid #dcdcdc;
        background: #fff;

    }

    .item-list-name {
        width: 80%;
    }

    .item-list-num {
        position: absolute;
        right: 0;
        top: 0;
        font-size: 14px;
    }
</style>